<template>
  <div class="cart">
    <van-nav-bar title="购物车">
      <template #left>
        <van-icon
          name="arrow-left"
          size=".6rem"
          @click="$router.back()"
          color="grey"
        />
      </template>
      <template #right>
        <van-icon name="ellipsis" size=".6rem" color="grey" />
      </template>
    </van-nav-bar>
    <van-swipe-cell v-for="(item, idx) in cartlist" :key="item._id">
      <van-row>
        <van-col span="3">
          <div class="cart-icon">
            <van-checkbox
              v-model="item.checked"
              @click="rowClick"
            ></van-checkbox>
          </div>
        </van-col>
        <van-col span="21">
          <van-card
            :price="item.good_info.price"
            :desc="item.good_info.desc"
            :title="item.good_info.name"
            class="goods-card"
            :thumb="item.good_info.img"
          >
            <template #num>
              <button @click="changeNum('sub', item)">-</button>
              <input type="text" v-model="item.num" />
              <button @click="changeNum('add', item)">+</button>
            </template>
          </van-card>
        </van-col>
      </van-row>
      <template #right>
        <van-button
          square
          text="删除"
          type="danger"
          style="height: 100%"
          @click="deletgood(item, idx)"
        />
      </template>
    </van-swipe-cell>
    <van-submit-bar :price="total" button-text="提交订单" @submit="onSubmit">
      <van-checkbox v-model="full" @click="fullClick">全选</van-checkbox>
    </van-submit-bar>
    <br />
    <br />
    <TabBar></TabBar>
  </div>
</template>

<script>
import { TabBar } from "@/components";
import { Dialog } from "vant";
import { Notify } from "vant";
export default {
  data() {
    return {
      checked: false,
      full: false,
      cartlist: [],
    };
  },
  components: {
    TabBar,
    [Dialog.Component.name]: Dialog.Component,
    [Notify.Component.name]: Notify.Component,
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.$api.fetchCartList().then((res) => {
        this.cartlist = res.list;
      });
    },
    isFull() {
      const arr = this.cartlist.filter((ele) => ele.checked);
      this.full = arr.length !== 0 && arr.length == this.cartlist.length;
    },
    rowClick() {
      this.isFull();
    },
    fullClick() {
      this.cartlist.map((ele, idx) => {
        this.cartlist[idx].checked = this.full;
      });
      // this.$forceUpdate()
      this.cartlist = JSON.parse(JSON.stringify(this.cartlist));
    },
    changeNum(type, item) {
      if (type == "add") {
        this.$api
          .fetchCartUpd({ cart_id: item._id, new_num: item.num + 1 })
          .then(() => {
            item.num += 1;
          });
      } else {
        if (item.num <= 1) {
          Notify({ message: "不能少于1件", duration: 800 });
          return;
        }
        this.$api
          .fetchCartUpd({ cart_id: item._id, new_num: item.num - 1 })
          .then(() => {
            item.num -= 1;
          });
      }
    },
    deletgood(item, idx) {
      Dialog.confirm({
        message: "确定要删除此商品吗",
      })
        .then(() => {
          console.log("调用删除接口");
          this.$api.fetchCartDel({ cart_id: item._id }).then(() => {
            this.cartlist.splice(idx, 1);
            this.isFull();
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    checkSubmit() {
      let flag = 0;
      if (this.total > 0) flag++;
      else this.$toast("请勾选你要购买的商品");
      return flag === 1;
    },
    onSubmit() {
      if (this.checkSubmit()) {
        let ids = "";
        this.cartlist.forEach((ele) => {
          if (ele.checked) ids += `;${ele._id}`;
        });
        this.$api.fetchCartSubmit({ ids }).then(() => this.init());
        this.isFull();
      }
    },
  },
  computed: {
    total() {
      let res = 0;
      this.cartlist.forEach((item) => {
        if (item.checked) {
          res += item.num * item.good_info.price;
        }
      });
      return res * 100;
    },
  },
};
</script>

<style lang="scss" scoped>
.cart {
  background-color: #f2f2f2;
  height: 16rem;
  padding-bottom: 1.333333rem;
}
.goods-card {
  margin: 0;
  background-color: white;
  padding-left: 0px;
}
.van-row {
  width: 9.973333rem;
  height: 2.773333rem;
  margin-bottom: 5px;
  background-color: white;
  border-radius: 10px;
  padding: 5px;
  margin-top: 10px;
}

.van-submit-bar {
  position: fixed;
  bottom: 1.333333rem;
  box-shadow: 0px 5px 7px -8px #5e5e5e;
}
.van-card__num {
  width: 1.6rem;
  height: 0.4rem;
  display: flex;
  line-height: 0.4rem;
}

.van-card__num input {
  text-align: center;
  flex: 1;
  width: 0.66rem;
  height: 0.4rem;
  border: none;
  outline: none;
}
.van-card__num button {
  border: none;
  width: 0.4rem;
  height: 0.4rem;
  text-align: center;
  line-height: 0.4rem;
  justify-content: space-between;
}
</style>
<style lang="scss" >
.cart-icon {
  height: 2.773333rem;
  display: grid;
  place-items: center;
  padding-right: 0px;
}
</style>